<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace longbingcore\wxcore;

class Winnerlook
{
    protected $accountid;
    protected $url;
    protected $secret;
    protected $reminder_text;
    protected $reminder_phone;
    protected $template_id;
    protected $help_tmpl_id;
    protected $config;
    protected static $uniacid;
    public function __construct($uniacid, $appid = "", $token = "")
    {
        self::$uniacid = $uniacid;
        $config_model = new \app\virtual\model\Config();
        $config = $config_model->dataInfo(["uniacid" => $uniacid]);
        $this->accountid = !empty($appid) ? $appid : $config["winnerlook_appid"];
        $this->url = "http://101.37.133.245:11108/";
        $this->secret = !empty($token) ? $token : $config["winnerlook_token"];
        $this->reminder_text = $config["reminder_text"];
        $this->reminder_phone = $config["moor_reminder_phone"];
        $this->template_id = $config["reminder_tmpl_id"];
        $this->help_tmpl_id = $config["help_tmpl_id"];
        $this->config = $config;
    }
    public function bindphone($phoneA, $phoneB, $phoneX, $order_id, $is_city = 0)
    {
        $time = date("YmdHis");
        $time = time() * 1000;
        $authorization = base64_encode($this->accountid . ":" . $time);
        $sig = md5($this->accountid . $this->secret . $time);
        $data = ["bindNumberA" => $phoneA, "bindNumberB" => $phoneB, "callbackUrl" => "http://" . $_SERVER["HTTP_HOST"] . "/virtual/CallBack/aliyunCallBackLook", "customerData" => $order_id, "maxBindingTime" => 60];
        if($is_city == 0) {
            $data["middleNumber"] = $phoneX;
        }
        $url = $this->url . "voice/1.0.0/middleNumberAXB/" . $this->accountid . "/" . $sig;
        $res = $this->curlPost($url, $data, $authorization);
        $res = !empty($res) ? json_decode($res, true) : [];
        return $res;
    }
    public function delBind($phoneA, $phoneB, $phoneX)
    {
        $time = time() * 1000;
        $authorization = base64_encode($this->accountid . ":" . $time);
        $sig = md5($this->accountid . $this->secret . $time);
        $url = $this->url . "voice/1.0.0/middleNumberUnbind/" . $this->accountid . "/" . $sig;
        $data = ["middleNumber" => $phoneX, "bindNumberA" => $phoneA, "bindNumberB" => $phoneB, "mode" => 0];
        $res = $this->curlPost($url, $data, $authorization);
        $res = !empty($res) ? json_decode($res, true) : [];
        return $res;
    }
    public function webCall($phone, $type = 1)
    {
        $time = time() * 1000;
        $authorization = base64_encode($this->accountid . ":" . $time);
        $sig = md5($this->accountid . $this->secret . $time);
        if($type == 1) {
            $tmpl_id = $this->config["reminder_tmpl_id"];
        } elseif($type == 2) {
            $tmpl_id = $this->config["help_tmpl_id"];
        } else {
            $tmpl_id = $this->config["order_end_tmpl_id"];
        }
        $data = ["templateId" => $tmpl_id, "calleeNumber" => $phone, "templateArgs" => (object) [], "replayTimes" => 1];
        if(!empty($this->config["winnerlook_phone"])) {
            $data["displayNumber"] = $this->config["winnerlook_phone"];
        }
        $url = $this->url . "voice/1.0.0/notify/" . $this->accountid . "/" . $sig;
        $res = $this->curlPost($url, $data, $authorization);
        $res = !empty($res) ? json_decode($res, true) : [];
        return $res;
    }
    public function getShortTmp()
    {
        $time = date("YmdHis");
        $authorization = base64_encode($this->accountid . ":" . $time);
        $sig = strtoupper(md5($this->accountid . $this->secret . $time));
        $url = $this->url . "/v20160818/sms/getSmsTemplate/" . $this->accountid . "?sig=" . $sig;
        $res = $this->curlPost($url, [], $authorization);
        $res = !empty($res) ? json_decode($res, true) : [];
        return $res;
    }
    public function sendShortMsg($phone, $msg)
    {
        $config_model = new \app\massage\model\ShortCodeConfig();
        $config = $config_model->dataInfo(["uniacid" => self::$uniacid]);
        $data["userCode"] = $config["winner_user"];
        $data["userPass"] = $config["winner_pass"];
        $data["DesNo"] = $phone;
        $data["Msg"] = $config["short_sign"] . $msg;
        $url = "http://118.178.116.15/winnerrxd/api/trigger/SendMsg";
        $res = lbCurlPost($url, $data);
        $res = simplexml_load_string($res);
        $res = json_encode($res);
        $res = json_decode($res, true);
        if(0 < $res[0]) {
            $arr["Message"] = "OK";
        } else {
            $arr["Message"] = "发送失败" . $res[0];
        }
        return $arr;
    }
    public function curlPost($url, $data, $authorization)
    {
        $header[] = "Accept: application/json";
        $header[] = "Content-type: application/json;charset='utf-8'";
        $header[] = "Content-Length: " . strlen(json_encode($data));
        $header[] = "Authorization: " . $authorization;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        $return = curl_exec($ch);
        if($return === false) {
            echo "CURL Error:" . curl_error($ch);
            exit;
        }
        curl_close($ch);
        return $return;
    }
}

?>